﻿Imports System.Threading.Tasks

Public Class frmSynchronization_Form
    Private WithEvents Socket As Connections
    Public is_edit As Boolean
    Public is_master As Boolean
    Private dbconfig() As Object
    Private master_table_list As DataTable
    Private auditlog_fieldname As String = "auditlog_timestamp"
    Private databases As New DataTable
    Private tables As New DataTable
    Private registerTask As Task
    Private WaitTask As String = "", WaitTaskResponse As Boolean = False, frmWaitTimeOutState As Boolean = False, WaitTaskResponseHandler As Integer = 0
    Private sysDateTime As String = "0000-00-00 00:00:00"
    Dim tindex As Long

    Private Sub loadContact(Lookup As DevExpress.XtraEditors.LookUpEdit)
        Dim data As DataTable = mysql_query("SELECT * FROM tb_contact")

        Lookup.Properties.DataSource = data
        Lookup.Properties.DisplayMember = "email"
        Lookup.Properties.ValueMember = "id_kontak"
    End Sub

    Private Sub loadServer(Lookup As DevExpress.XtraEditors.LookUpEdit)
        Dim data As DataTable = mysql_query("SELECT id_host,nama_host,host,port,username FROM tb_host")

        Lookup.Properties.DataSource = data
        Lookup.Properties.DisplayMember = "nama_host"
        Lookup.Properties.ValueMember = "id_host"
    End Sub

    Private Sub loadTableMaster(CheckedList As DevExpress.XtraEditors.CheckedListBoxControl)
        Dim data As DataTable
        Dim query As String
        query = "SHOW TABLES FROM " & dbconfig(4).ToString & " WHERE Tables_in_" & dbconfig(4).ToString & " NOT LIKE 'auditlog_%'"
        data = mysql_query(query, -1, False, dbconfig(0).ToString, CType(dbconfig(1).ToString, Long), dbconfig(2).ToString, dbconfig(3).ToString, dbconfig(4).ToString)
        master_table_list = data
        With CheckedList
            .DataSource = data
            .DisplayMember = "Tables_in_" & dbconfig(4).ToString
            .ValueMember = "Tables_in_" & dbconfig(4).ToString
        End With
        
    End Sub

    Private Sub loadDBMaster(Lookup As DevExpress.XtraEditors.LookUpEdit)
        Dim data As DataTable
        data = mysql_show_databases(False, dbconfig(0).ToString, CType(dbconfig(1).ToString, Long), dbconfig(2).ToString, dbconfig(3).ToString)
        With Lookup.Properties
            .DataSource = data
            .DisplayMember = "Database"
            .ValueMember = "Database"
        End With
    End Sub

    Private Sub frmSynchronization_Form_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        Socket = Nothing
    End Sub

    Private Sub frmSynchronization_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadServer(LookUpEditMaster)
        loadContact(LookUpEditContactSlave)
        Try
            Socket = SockGlobal
            If frmMain.iConnect.Caption = "Connect" Or Socket Is Nothing Then
                XtraTabPageSlave.PageEnabled = False
            Else
                XtraTabPageSlave.PageEnabled = True
            End If
        Catch ex As Exception
            XtraTabPageSlave.PageEnabled = False
        End Try
    End Sub

    Private Sub XtraTabControlOperation_Click(sender As Object, e As EventArgs) Handles XtraTabControlOperation.Click
        If XtraTabControlOperation.SelectedTabPageIndex = 0 Then
            loadServer(LookUpEditMaster)
        End If
    End Sub

    Private Sub LookUpEditMaster_EditValueChanged(sender As Object, e As EventArgs) Handles LookUpEditMaster.EditValueChanged
        Dim data As DataTable = mysql_query(String.Format("SELECT * FROM tb_host WHERE id_host = '{0}'", LookUpEditMaster.EditValue.ToString))
        dbconfig = New Object() {data.Rows(0)("host"), data.Rows(0)("port"), data.Rows(0)("username"), data.Rows(0)("password"), "", data.Rows(0)("id_host")}
        loadDBMaster(LookUpEditMasterDB)
    End Sub

    Private Sub LookUpEditMasterDB_EditValueChanged(sender As Object, e As EventArgs) Handles LookUpEditMasterDB.EditValueChanged
        Dim data As DataTable = mysql_query(String.Format("SELECT * FROM tb_host WHERE id_host = '{0}'", LookUpEditMaster.EditValue.ToString))
        dbconfig = New Object() {data.Rows(0)("host"), data.Rows(0)("port"), data.Rows(0)("username"), data.Rows(0)("password"), LookUpEditMasterDB.EditValue.ToString, data.Rows(0)("id_host")}
        loadTableMaster(CheckedListBoxControlMaster)
    End Sub


    Private Sub CheckedListBoxControlMaster_ItemCheck(sender As Object, e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles CheckedListBoxControlMaster.ItemCheck
        If CheckedListBoxControlMaster.ItemCount > 0 Then
            Dim countChecked As Integer = 0
            For i As Integer = 0 To CheckedListBoxControlMaster.ItemCount - 1
                If CheckedListBoxControlMaster.GetItemChecked(i) = True Then
                    countChecked = countChecked + 1
                End If
            Next

            If countChecked = CheckedListBoxControlMaster.ItemCount Then
                CheckBoxAll.Checked = True
            Else
                CheckBoxAll.Checked = False
            End If
        End If
    End Sub

    Private Sub CheckedListBoxControlMaster_MouseMove(sender As Object, e As MouseEventArgs) Handles CheckedListBoxControlMaster.MouseMove
        If tindex <> CheckedListBoxControlMaster.IndexFromPoint(e.Location) Then
            tindex = CheckedListBoxControlMaster.IndexFromPoint(CheckedListBoxControlMaster.PointToClient(MousePosition))
            If tindex > -1 Then
                'ToolTipControllerCheckedMaster.SetToolTip(CheckedListBoxControlMaster, master_table_list.Rows(tindex)("timestamp").ToString)
                'ToolTipControllerCheckedMaster.ShowHint(ToolTipControllerCheckedMaster.GetToolTip(CheckedListBoxControlMaster), DevExpress.Utils.ToolTipLocation.RightCenter, MousePosition)
            End If
        End If
    End Sub

    Private Sub SimpleButtonSlaveSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSlaveSave.Click
        WaitTask = "SlaveSave"
        createWaitTask()
        Socket.sendMessages("/dbsyncregister" & Packet.pSplit & LookUpEditSlaveDB.EditValue.ToString, LookUpEditContactSlave.Text)
        frmWait.Dispose()
        frmWait.SetOptions("Please Wait", "Registering data...", 10000)
        frmWait.ShowDialog()
    End Sub

    Private Sub createWaitTask()
        WaitTaskResponse = False
        WaitTaskResponseHandler = 0
        registerTask = Task.Factory.StartNew(AddressOf doWaitTask, TaskCreationOptions.LongRunning)
    End Sub

    Private Sub doWaitTask()
        Dim dbquery As New cMySQLDatabase
        Do
            Me.Invoke(Sub() frmWaitTimeOutState = frmWait.TimeOutState)
            If frmWaitTimeOutState = True Then
                Me.Invoke(Sub() MsgBox("Request Time Out"))
                Exit Do
            Else
                Select Case WaitTask
                    Case "SlaveSave"
                        If WaitTaskResponse Then
                            
                            Me.Invoke(Sub() frmWait.Dispose())
                            Me.Invoke(Sub() frmWait.Close())
                            Select Case WaitTaskResponseHandler
                                Case 0
                                    Dim obj As Object
                                    dbquery.mysql_begin_transaction()
                                    obj = dbquery.mysql_transaction_non_query(String.Format("INSERT INTO tb_syncconfig_slave(`id_kontak`,`master_id`,`database`) VALUES ('{0}','{1}','{2}')", LookUpEditContactSlave.EditValue.ToString, LookUpEditSlaveDB.EditValue.ToString, LookUpEditSlaveDB.Text))
                                    If obj Then
                                        obj = dbquery.mysql_transaction_query("SELECT LAST_INSERT_ID()", 0)
                                        If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                                            For i As Integer = 0 To CheckedListBoxControlSlaveTable.ItemCount - 1
                                                dbquery.mysql_transaction_non_query(String.Format("INSERT INTO tb_syncconfig_slave_detail(id_syncconfig_slave,table_name,last_timestamp) VALUES ('{0}','{1}','{2}')", obj.ToString, CheckedListBoxControlSlaveTable.GetItemValue(i), sysDateTime))
                                            Next
                                        End If
                                    End If
                                    dbquery.mysql_commit_transaction()
                                    dbquery.mysql_end_transaction()
                                    Me.Invoke(Sub() frmSynchronization.loadData("Master"))
                                    Me.Invoke(Sub() frmSynchronization.loadData("Slave"))
                                    Me.Invoke(Sub() Me.Close())
                                Case 1
                                    Me.Invoke(Sub() MsgBox("Unknown Contact"))
                                Case 2
                                    Me.Invoke(Sub() MsgBox("Contact is Offline"))
                                Case 3
                                    Me.Invoke(Sub() MsgBox("Save Failed" & vbCrLf & "Reason: Unknown Response"))
                                Case 11
                                    Me.Invoke(Sub() MsgBox("Save Failed" & vbCrLf & "Reason: Server can't identify the requested master data"))
                                Case 12
                                    Me.Invoke(Sub() MsgBox("Save Failed" & vbCrLf & "Reason: Contact not registered to Master"))
                                Case 13
                                    Me.Invoke(Sub() MsgBox("Save Failed" & vbCrLf & "Reason: Failed to Link Slave to Master"))
                                Case 19
                                    Me.Invoke(Sub() MsgBox("Save Failed" & vbCrLf & "Reason: Server cannot complete your request"))
                                Case Else

                            End Select
                            Exit Do
                        End If
                    Case "SlaveDBRequest"
                        If WaitTaskResponse Then
                            Me.Invoke(Sub() frmWait.Dispose())
                            Me.Invoke(Sub() frmWait.Close())
                            Select Case WaitTaskResponseHandler
                                Case 0
                                    Me.Invoke(Sub() SimpleButtonSlaveSave.Enabled = True)
                                Case 1
                                    Me.Invoke(Sub() MsgBox("Unknown Contact"))
                                    Me.Invoke(Sub() SimpleButtonSlaveSave.Enabled = False)
                                Case 2
                                    Me.Invoke(Sub() MsgBox("Contact is Offline"))
                                    Me.Invoke(Sub() SimpleButtonSlaveSave.Enabled = False)
                                Case 3
                                    Me.Invoke(Sub() MsgBox("Cannot Get Configuration List From Master Target"))
                                    Me.Invoke(Sub() SimpleButtonSlaveSave.Enabled = False)
                                Case Else
                            End Select
                            Exit Do
                        End If
                    Case Else
                        Exit Do
                End Select
            End If
            registerTask.Wait(1000)
        Loop While (1)
    End Sub

    Private Sub SimpleButtonMasterSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonMasterSave.Click
        Cursor = Cursors.WaitCursor
        If dbconfig(4).Equals("") Then
            MessageBox.Show("Please Select Master Database")
            Exit Sub
        End If
        Dim field As DataTable = mysql_query("SELECT * FROM tb_syncconfig WHERE id_host='" & dbconfig(5) & "' AND `database`='" & dbconfig(4) & "'")
        Dim field_datatype As DataTable
        Dim f_name As String = "", f_pk As String = "", f_type As String = "", _
            f_olddata As String = "", f_data As String = ""
        Dim quotes As String = ""
        Dim quotes2 As String = "','"
        Dim status As Boolean = False
        Dim id_syncconfig As String
        Dim checkedItemsCount As Integer = 0
        Dim mysql As New cMySQLDatabase

        For i As Integer = 0 To CheckedListBoxControlMaster.ItemCount - 1
            If CheckedListBoxControlMaster.GetItemChecked(i) = True Then
                checkedItemsCount = checkedItemsCount + 1
            End If
        Next

        If field.Rows.Count > 0 Then
            MsgBox("Configuration That Using This Host and Database Already Exists")
            Cursor = Cursors.Default
            Exit Sub
        ElseIf CheckedListBoxControlMaster.ItemCount <= 0 Or checkedItemsCount <= 0 Then
            MsgBox("No Table Loaded Or Nothing Selected")
            Cursor = Cursors.Default
            Exit Sub
        End If

        mysql.mysql_begin_transaction()
        status = mysql.mysql_transaction_non_query("INSERT INTO tb_syncconfig(`id_host`,`database`) VALUES ('" & dbconfig(5) & "','" & dbconfig(4) & "')")
        If status Then
            id_syncconfig = mysql.mysql_transaction_query("SELECT LAST_INSERT_ID()", 0)
            mysql.mysql_commit_transaction()
            mysql.mysql_end_transaction()
            For i As Integer = 0 To CheckedListBoxControlMaster.ItemCount - 1
                If CheckedListBoxControlMaster.GetItemChecked(i) = True Then
                    'Cek Field Timestamp pada Tabel

                    'Cek Tabel audit log
                    field = mysql_query("SHOW TABLES FROM " & dbconfig(4) & " WHERE Tables_in_" & dbconfig(4) & " = 'auditlog_" & dbconfig(4) & "'", -1, False, dbconfig(0), dbconfig(1), dbconfig(2), dbconfig(3), dbconfig(4))
                    'Tambah Tabel audit log
                    If field.Rows.Count <= 0 Then
                        status = mysql_non_query("CREATE TABLE auditlog_" & dbconfig(4) & " (execution_id BIGINT PRIMARY KEY AUTO_INCREMENT,timestamp TIMESTAMP,table_name VARCHAR(100),action CHAR(1),field TEXT,field_pk TEXT,field_type TEXT,old_data TEXT,new_data TEXT)", False, dbconfig(0), dbconfig(1), dbconfig(2), dbconfig(3), dbconfig(4))
                    End If

                    'Cek Nama Trigger
                    If status Then
                        field_datatype = mysql_query("SELECT COLUMN_NAME,IS_NULLABLE,DATA_TYPE,COLUMN_KEY FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '" & dbconfig(4) & "' AND table_name = '" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "' ORDER BY ORDINAL_POSITION ASC", -1, False, dbconfig(0), dbconfig(1), dbconfig(2), dbconfig(3), dbconfig(4))
                        f_name = ""
                        f_type = ""
                        f_pk = ""
                        f_data = ""
                        f_olddata = ""
                        quotes = ","
                        quotes2 = "','"
                        For x As Integer = 0 To field_datatype.Rows.Count - 1
                            If x >= field_datatype.Rows.Count - 1 Then
                                quotes = vbNullString
                                quotes2 = vbNullString
                            End If

                            f_name = f_name + field_datatype.Rows(x)("COLUMN_NAME") + quotes
                            'Apakah field primary?
                            If field_datatype.Rows(x)("COLUMN_KEY").ToString.IndexOf("PRI") > -1 Then
                                f_pk = f_pk + field_datatype.Rows(x)("COLUMN_NAME") + quotes
                            End If

                            f_type = f_type + field_datatype.Rows(x)("DATA_TYPE") + quotes

                            If field_datatype.Rows(x)("IS_NULLABLE").Equals("YES") Then
                                f_olddata = f_olddata + String.Format("IF(OLD.{0} IS NULL,'(NULL)',OLD.{0})", field_datatype.Rows(x)("COLUMN_NAME")) + quotes + quotes2 + quotes
                                f_data = f_data + String.Format("IF(NEW.{0} IS NULL,'(NULL)',NEW.{0})", field_datatype.Rows(x)("COLUMN_NAME")) + quotes + quotes2 + quotes
                            Else
                                f_olddata = f_olddata + "OLD." & field_datatype.Rows(x)("COLUMN_NAME") + quotes + quotes2 + quotes
                                f_data = f_data + "NEW." & field_datatype.Rows(x)("COLUMN_NAME") + quotes + quotes2 + quotes
                            End If

                        Next

                        If f_pk.Equals("") = False Then
                            f_pk = f_pk.Substring(0, f_pk.Length - 1)
                        End If

                        mysql_execute_script("DROP TRIGGER IF EXISTS `insert_auditlog_" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` $$ CREATE TRIGGER `insert_auditlog_" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` AFTER INSERT ON `" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` " & _
                                        "FOR EACH ROW BEGIN " & _
                                        "DECLARE f_name TEXT DEFAULT '" & f_name & "'; " & _
                                        "DECLARE f_type TEXT DEFAULT '" & f_type & "'; " & _
                                        "DECLARE f_pk TEXT DEFAULT '" & f_pk & "'; " & _
                                        "DECLARE f_data TEXT DEFAULT CONCAT(" & f_data & "); " & _
                                        "INSERT INTO auditlog_" & dbconfig(4) & "(`table_name`,`action`,`field`,`field_pk`,`field_type`,`old_data`,`new_data`) VALUES ('" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "','I',f_name,f_pk,f_type,NULL,f_data); " & _
                                        "END $$", False, dbconfig(0), dbconfig(1), dbconfig(2), dbconfig(3), dbconfig(4))
                        mysql_execute_script("DROP TRIGGER IF EXISTS `update_auditlog_" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` $$ CREATE TRIGGER `update_auditlog_" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` AFTER UPDATE ON `" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` " & _
                                        "FOR EACH ROW BEGIN " & _
                                        "DECLARE f_name TEXT DEFAULT '" & f_name & "'; " & _
                                        "DECLARE f_type TEXT DEFAULT '" & f_type & "'; " & _
                                        "DECLARE f_pk TEXT DEFAULT '" & f_pk & "'; " & _
                                        "DECLARE f_olddata TEXT DEFAULT CONCAT(" & f_olddata & "); " & _
                                        "DECLARE f_data TEXT DEFAULT CONCAT(" & f_data & "); " & _
                                        "INSERT INTO auditlog_" & dbconfig(4) & "(`table_name`,`action`,`field`,`field_pk`,`field_type`,`old_data`,`new_data`) VALUES ('" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "','U',f_name,f_pk,f_type,f_olddata,f_data); " & _
                                        "END $$", False, dbconfig(0), dbconfig(1), dbconfig(2), dbconfig(3), dbconfig(4))
                        mysql_execute_script("DROP TRIGGER IF EXISTS `delete_auditlog_" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` $$ CREATE TRIGGER `delete_auditlog_" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` AFTER DELETE ON `" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "` " & _
                                        "FOR EACH ROW BEGIN " & _
                                        "DECLARE f_name TEXT DEFAULT '" & f_name & "'; " & _
                                        "DECLARE f_type TEXT DEFAULT '" & f_type & "'; " & _
                                        "DECLARE f_pk TEXT DEFAULT '" & f_pk & "'; " & _
                                        "DECLARE f_olddata TEXT DEFAULT CONCAT(" & f_olddata & "); " & _
                                        "INSERT INTO auditlog_" & dbconfig(4) & "(`table_name`,`action`,`field`,`field_pk`,`field_type`,`old_data`,`new_data`) VALUES ('" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "','D',f_name,f_pk,f_type,f_olddata,NULL); " & _
                                        "END $$", False, dbconfig(0), dbconfig(1), dbconfig(2), dbconfig(3), dbconfig(4))

                        mysql_non_query("INSERT INTO tb_syncconfig_detail(`id_syncconfig`,`table_name`) VALUES ('" & id_syncconfig & "','" & CheckedListBoxControlMaster.GetItemValue(i).ToString & "')")
                        frmSynchronization.loadData("Master")
                        Me.Close()
                    End If
                End If
            Next
        Else
            mysql.mysql_end_transaction()
        End If
        Cursor = Cursors.Default
    End Sub

    Private Sub SimpleButtonExit_Click(sender As Object, e As EventArgs) Handles SimpleButtonExit.Click
        Me.Close()
    End Sub

    Private Sub CheckBoxAll_Click(sender As Object, e As EventArgs) Handles CheckBoxAll.Click
        If CheckBoxAll.Checked Then
            CheckedListBoxControlMaster.CheckAll()
        Else
            CheckedListBoxControlMaster.UnCheckAll()
        End If
    End Sub

    Private Sub Socket_dataResponse(response() As String) Handles Socket.dataResponse
        If response.Length > 1 Then
            Select Case response(0)
                Case "100" 'Messages Received
                    Select Case response(2)
                        Case "/responseunknowncontact"
                            'Unknown Contact Response From Server
                            Me.Invoke(Sub() WaitTaskResponse = True)
                            Me.Invoke(Sub() WaitTaskResponseHandler = 1)
                        Case "/responseofflinecontact"
                            'Offline Contact Response From Server
                            Me.Invoke(Sub() WaitTaskResponse = True)
                            Me.Invoke(Sub() WaitTaskResponseHandler = 2)
                        Case "/dbsyncregisterresponse"
                            'Register Response
                            Select Case response(3)
                                Case "0"
                                    Me.Invoke(Sub() WaitTaskResponse = True)
                                    Me.Invoke(Sub() WaitTaskResponseHandler = 0)
                                    Me.Invoke(Sub() sysDateTime = response(4))
                                Case "1"
                                    Me.Invoke(Sub() WaitTaskResponse = True)
                                    Me.Invoke(Sub() WaitTaskResponseHandler = 11)
                                Case "2"
                                    Me.Invoke(Sub() WaitTaskResponse = True)
                                    Me.Invoke(Sub() WaitTaskResponseHandler = 12)
                                Case "3"
                                    Me.Invoke(Sub() WaitTaskResponse = True)
                                    Me.Invoke(Sub() WaitTaskResponseHandler = 13)
                                Case "9"
                                    Me.Invoke(Sub() WaitTaskResponse = True)
                                    Me.Invoke(Sub() WaitTaskResponseHandler = 19)
                                Case Else
                                    Me.Invoke(Sub() WaitTaskResponse = True)
                                    Me.Invoke(Sub() WaitTaskResponseHandler = 3)
                            End Select
                        Case "/dbsyncconfigresponse" 'Response Code For Database Request
                            Me.Invoke(Sub() frmWaitTimeOutState = frmWait.TimeOutState)
                            If frmWaitTimeOutState = True Then
                                Exit Sub
                            End If
                            If response(3).Length > 1 Then
                                If response(3).Split("#").Length >= 1 Then
                                    Dim i As Integer = 0, j As Integer = 0
                                    Dim str() As String = response(3).Split("#")
                                    databases.Columns.Clear()
                                    databases.Rows.Clear()
                                    tables.Columns.Clear()
                                    tables.Rows.Clear()
                                    Me.Invoke(Sub() LookUpEditSlaveDB.EditValue = vbNull)
                                    Me.Invoke(Sub() CheckedListBoxControlSlaveTable.Items.Clear())
                                    databases.Columns.Add("database")
                                    databases.Columns.Add("master_id")
                                    tables.Columns.Add("database")
                                    tables.Columns.Add("table_name")
                                    For i = 0 To response(3).Split("#").Length - 1
                                        Me.Invoke(Sub() databases.Rows.Add(str(i).Split("*")(0).ToString, str(i).Split("*")(1).ToString))
                                        For j = 0 To str(i).Split("*")(2).Split("~").Length - 1
                                            tables.Rows.Add(str(i).Split("*")(0), str(i).Split("*")(2).Split("~")(j).ToString)
                                        Next
                                    Next
                                    Me.Invoke(Sub() LookUpEditSlaveDB.Properties.DataSource = databases)
                                    Me.Invoke(Sub() LookUpEditSlaveDB.Properties.DisplayMember = "database")
                                    Me.Invoke(Sub() LookUpEditSlaveDB.Properties.ValueMember = "master_id")
                                End If
                                Me.Invoke(Sub() WaitTaskResponse = True)
                                Me.Invoke(Sub() WaitTaskResponseHandler = 0)
                            Else
                                Me.Invoke(Sub() WaitTaskResponse = True)
                                Me.Invoke(Sub() WaitTaskResponseHandler = 3)
                            End If
                        Case Else
                            Me.Invoke(Sub() frmMain.Console_Append("PESAN: " & response(2)))
                    End Select
            End Select
        End If
    End Sub

    Private Sub LookUpEditContactSlave_EditValueChanged(sender As Object, e As EventArgs) Handles LookUpEditContactSlave.EditValueChanged
        Dim data As DataTable = mysql_query(String.Format("SELECT * FROM tb_contact WHERE id_kontak = '{0}'", LookUpEditContactSlave.EditValue.ToString))
        Socket.sendMessages("/dbsyncgetconfig", data.Rows(0)("email"))
        LookUpEditSlaveDB.Properties.DataSource = vbNull
        LookUpEditSlaveDB.EditValue = vbNull
        CheckedListBoxControlSlaveTable.Items.Clear()
        WaitTask = "SlaveDBRequest"
        createWaitTask()
        frmWait.Dispose()
        frmWait.SetOptions("Please Wait", "Requesting Data to Server...", 10000)
        frmWait.ShowDialog()
        
    End Sub

    Private Sub LookUpEditSlaveDB_EditValueChanged(sender As Object, e As EventArgs) Handles LookUpEditSlaveDB.EditValueChanged
        If LookUpEditSlaveDB.Text.Equals("") Then Exit Sub
        CheckedListBoxControlSlaveTable.Items.Clear()
        For i As Integer = 0 To tables.Rows.Count - 1
            If LookUpEditSlaveDB.Text = tables.Rows(i)("database") Then
                CheckedListBoxControlSlaveTable.Items.Add(tables.Rows(i)("table_name"))
            End If
        Next
        CheckedListBoxControlSlaveTable.CheckAll()
    End Sub

    Private Sub CheckedListBoxControlSlaveTable_DrawItem(sender As Object, e As DevExpress.XtraEditors.ListBoxDrawItemEventArgs) Handles CheckedListBoxControlSlaveTable.DrawItem
        'CheckedListBoxControlSlaveTable.Items(e.Index).Enabled = False
    End Sub


End Class